"""
---------------------------------------------------------------------
- File: schemas.py
- Date: 2025/7/12
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: Pydantic 数据校验模型，用于请求体验证、响应体序列化和数据交换
---------------------------------------------------------------------
"""

from datetime import datetime
from enum import Enum

from pydantic import BaseModel, Field, field_serializer, EmailStr
from tortoise.contrib.pydantic import pydantic_model_creator


class Item01(BaseModel):
    name: str
    des: str


class UserInfo(BaseModel):
    userId: int
    userName: str
    statu: bool


class ItemJson(BaseModel):
    title: str
    time: datetime

    @field_serializer("time")
    def serialize_time(self, time: datetime):
        return time.strftime("%Y--%m--%d %H:%M:%S")


class ModelName(str, Enum):
    r = "red"
    g = "green"
    b = "blue"


class BodyItem(BaseModel):
    name: str = Field(title="用户名")
    des: str
    price: float
    tax: float | None = None
    tag: set[str] = set()

    model_config = {
        "json_schema_extra": {
            "example": {
                "name": "yangzhide",
                "des": "黔东南",
                "price": 35.4,
                "tax": 3.2,
                "tag": ["1", "2", "3", "4", "3", "4", "99"],
            }
        }
    }


class UserBase(BaseModel):
    username: str
    email: EmailStr | str
    full_name: str | None = None


class UserIn(UserBase):
    password: str


class UserOut(UserBase):
    pass


class UserInDB(UserBase):
    hashed_password: str


class Item3(BaseModel):
    name: str | None = None
    description: str | None = None
    price: float | None = None
    tax: float = 10.5
    tags: list[str] = []
